package com.java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java.enums.UserBindEnum;
import com.java.exceptioin.Assert;
import com.java.hfb.FormHelper;
import com.java.hfb.HfbConst;
import com.java.hfb.RequestHelper;
import com.java.mapper.UserInfoMapper;
import com.java.pojo.entity.UserBind;
import com.java.mapper.UserBindMapper;
import com.java.pojo.entity.UserInfo;
import com.java.pojo.vo.UserBindVo;
import com.java.result.ResponseEnum;
import com.java.service.UserBindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.service.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author Helen
 * @since 2021-07-08
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {

    @Resource
    private UserBindMapper userBindMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public String commitBindUser(UserBindVo userBindVo, Long userId) {

        //不同的user_id 相同的身份证就不允许
        QueryWrapper<UserBind> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_card",userBindVo.getIdCard())
                .ne("user_id",userId);
        UserBind userBind1 = userBindMapper.selectOne(queryWrapper);
        Assert.isNull(userBind1, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);


        //在绑定用户记录之前现判断数据库中是否已经有该用户的绑定记录 通过userId查询
        QueryWrapper<UserBind> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        UserBind userBind = userBindMapper.selectOne(wrapper);
        //如果userBind1不能存在就创建
        if(userBind == null){
            //创建用户绑定记录对象
            userBind = new UserBind();
            BeanUtils.copyProperties(userBindVo,userBind);
            userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
            userBind.setUserId(userId);
            userBindMapper.insert(userBind);
        }else{
            //如果存在就更新当前数据
            BeanUtils.copyProperties(userBindVo,userBind);
            userBindMapper.updateById(userBind);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("agentId", HfbConst.AGENT_ID);
        map.put("agentUserId",userId);
        map.put("idCard",userBindVo.getIdCard());
        map.put("personalName", userBindVo.getName());
        map.put("bankType", userBindVo.getBankType());
        map.put("bankNo", userBindVo.getBankNo());
        map.put("mobile", userBindVo.getMobile());
        map.put("returnUrl", HfbConst.USERBIND_RETURN_URL);
        map.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);
        map.put("timestamp", RequestHelper.getTimestamp());
        String sign = RequestHelper.getSign(map);//获取签名
        map.put("sign",sign);
        String form = FormHelper.buildForm(HfbConst.USERBIND_URL, map);

        return form;
    }

    //账户绑定异步回调
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void notifybind(Map<String, Object> map) {
        //里面获取bindCode agentUserId
        String bindCode = (String)map.get("bindCode");
        String userId = (String)map.get("agentUserId");
        //查询userBind表中的用户 设置绑定状态和用户状态
        QueryWrapper<UserBind> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        UserBind userBind = userBindMapper.selectOne(wrapper);

        userBind.setBindCode(bindCode);
        userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
        //更新userBind
        userBindMapper.updateById(userBind);

        //更新userInfo
        UserInfo userInfo = userInfoMapper.selectById(userId);
        userInfo.setBindCode(bindCode);
        userInfo.setIdCard(userBind.getIdCard());
        userInfo.setName(userBind.getName());
        userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public String getBindCodeByUserId(Long userId) {

        QueryWrapper<UserBind> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        UserBind userBind = userBindMapper.selectOne(wrapper);
        return userBind.getBindCode();

    }
}
